home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Snippets / OS Utilities / TimeZone.Daylight / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-16  |  6.0 KB  |  280 lines  |  [TEXT/CWIE]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    Machine Location                                        */
  4. /*                                                                            */
  5. /*    Description:    draws corrent location and time zone information.        */
  6. /*    Description:    shows  a function that determines if Daylight            */
  7. /*    Description:    savings time is turned on or off from the Date & Time    */
  8. /*    Description:    control panel.  Inside Mac: OS Utililies Chapter 4         */
  9. /*    Description:    should be consulted for further information.            */
  10. /*                                                                            */
  11. /*                                                                            */
  12. /*    Programmer:        David Hayward                                            */
  13. /*    Modified by:    Albert Hui                                                */
  14. /*    Organization:    Apple Computer, Inc.                                    */
  15. /*    Department:        Developer Technical Support, DTS                        */
  16. /*    Date Created:    6-16-94                                                    */
  17. /*    Last Modified:    8-16-96                                                    */
  18. /*                                                                            */
  19. /****************************************************************************/
  20.  
  21. #include <Dialogs.h>
  22. #include <Fonts.h>
  23. #include <Icons.h>
  24. #include <Memory.h>
  25. #include <Types.h>
  26. #include <ToolUtils.h>
  27. #include <Windows.h>
  28. #include <FixMath.h>
  29. #include <QuickDraw.h>
  30. #include <GestaltEqu.h>
  31. #include <OSUtils.h>
  32. #include "initMac.h"
  33.  
  34.  
  35. /*\
  36. |*| ---------------------------------------------------------------------
  37. |*| GLOBALS
  38. |*| ---------------------------------------------------------------------
  39. \*/
  40. WindowPtr        gWindow;
  41. MachineLocation    gLocation;
  42. Boolean            done=0;
  43.  
  44.  
  45. /*\
  46. |*| ---------------------------------------------------------------------
  47. |*| PROTOTYPES
  48. |*| ---------------------------------------------------------------------
  49. \*/
  50. void main                (void);
  51. void createWindow        (void);
  52. void DoEventLoop        (void);
  53. void DoUpdate            (WindowPtr whichWindow);
  54. void DoMouseDown        (EventRecord event);
  55. void DrawHMS (long time);
  56. void DrawDMS (long degr);
  57. int IsDaylightSavingsOn();
  58.  
  59. /*\
  60. |*| ---------------------------------------------------------------------
  61. |*| Determine if Daylight Savings Time is on
  62. |*| ---------------------------------------------------------------------
  63. \*/
  64.  
  65. int IsDaylightSavingsOn()
  66. {
  67.     int retVal = 0;
  68.     MachineLocation    theLocation;
  69.  
  70.     
  71.     ReadLocation(&theLocation);
  72.     if (theLocation.u.dlsDelta == (signed char) 0x80) {
  73.         retVal = 1;
  74.     }
  75.     return(retVal);
  76. }        
  77.             
  78.  
  79.  
  80. void main (void)
  81. {
  82.     InitToolBox(1);
  83.  
  84.     createWindow();
  85.     
  86. ReadLocation(&gLocation);
  87.     
  88.     DoEventLoop();
  89. }
  90.  
  91.  
  92. void createWindow (void)
  93. {
  94.     gWindow = GetNewCWindow(128,nil,nil);
  95.     SetPort( gWindow );
  96. }
  97.  
  98.  
  99. /*\
  100. |*| ---------------------------------------------------------------------
  101. |*| DoEventLoop()
  102. |*| ---------------------------------------------------------------------
  103. \*/
  104. void DoEventLoop (void)
  105. {
  106.     EventRecord event;
  107.     WindowPtr   window;
  108.     long        mssg;
  109.  
  110.     while ( !done ) 
  111.     {
  112.         if (WaitNextEvent( everyEvent, &event, 60, nil ))
  113.         {
  114.             mssg = event.message;
  115.             switch (event.what)
  116.             {
  117.                 case mouseDown :                        /* handle mouse clicks */
  118.                     DoMouseDown (event);
  119.                     break;
  120.                 
  121.                 case keyDown :                            /* handle key hits */
  122.                 case autoKey :
  123.                     break;
  124.                     
  125.                 case updateEvt :                        /* handle update events */
  126.                     DoUpdate((WindowPtr)mssg);
  127.                     break;
  128.                 
  129.                 case osEvt:                                /* handle os events */
  130.                     if ( (mssg>>24)                        /* if high byte of message indicates */
  131.                           == suspendResumeMessage )        /* this is suspend/resume event */
  132.                     {
  133.                         if (mssg & resumeFlag)            /* if resume event */
  134.                         {
  135.                             /* we're switching back from another app so */
  136.                             /* we may need to show floating window */
  137.                         //    InvalRect(&((GrafPtr)gWindow)->portRect);
  138.                         }
  139.                     }
  140.                     break;
  141.             }
  142.         }
  143.         else            /* null event */
  144.         {
  145.             MachineLocation temp;
  146.             
  147.             ReadLocation(&temp);
  148.             
  149.             if ( (gLocation.latitude != temp.latitude) ||
  150.                  (gLocation.longitude != temp.longitude) ||
  151.                  (gLocation.u.gmtDelta != temp.u.gmtDelta) )
  152.             {
  153.                 gLocation = temp;
  154.                 InvalRect(&((GrafPtr)gWindow)->portRect);
  155.             }
  156.         }
  157.     }
  158. }
  159.  
  160.  
  161. /*\
  162. |*| ---------------------------------------------------------------------
  163. |*| DoMouseDown
  164. |*| handle DoMouseDown events
  165. |*| ---------------------------------------------------------------------
  166. \*/
  167. void DoMouseDown (EventRecord event)
  168. {
  169.     WindowPtr   window;
  170.     short       clickArea;
  171.     Rect        screenRect;
  172.  
  173.     clickArea = FindWindow( event.where, &window );
  174.     
  175.     switch (clickArea)
  176.     {
  177.         case inDrag:
  178.             screenRect = (**GetGrayRgn()).rgnBBox;
  179.             DragWindow( window, event.where, &screenRect );
  180.             break;
  181.  
  182.         case inContent:
  183.             if (window != FrontWindow())
  184.                 SelectWindow( window );
  185.             break;
  186.         
  187.         case inGoAway:
  188.             if (TrackGoAway( window, event.where ))
  189.                 done = 1;
  190.             break;
  191.     }
  192. }
  193.             
  194.             
  195. /*\
  196. |*| ---------------------------------------------------------------------
  197. |*| DoUpdate
  198. |*| handle update events
  199. |*| ---------------------------------------------------------------------
  200. \*/
  201. void DoUpdate (WindowPtr window)
  202. {
  203.     Str255    str;
  204.     long    gmt;
  205.     
  206.     SetPort( window );
  207.     BeginUpdate( window );
  208.  
  209.     EraseRect( &((GrafPtr)window)->portRect);
  210.     
  211.     MoveTo(10,20);
  212.     DrawString("\pLat: ");
  213.     DrawDMS((long)gLocation.latitude);
  214.     
  215.     MoveTo(10,40);
  216.     DrawString("\pLong: ");
  217.     DrawDMS((long)gLocation.longitude);
  218.     
  219.     MoveTo(10,60);
  220.     DrawString("\pdlsΔ: ");
  221.     NumToString( (long)gLocation.u.dlsDelta, str);
  222.     DrawString(str);
  223.     
  224.     if (IsDaylightSavingsOn()) {
  225.         DrawString("\p Daylight On ");
  226.     } else {
  227.         DrawString("\p Daylight Off ");
  228.     }
  229.     
  230.     MoveTo(10,80);
  231.     DrawString("\pgmtΔ: ");
  232.     gmt = gLocation.u.gmtDelta;
  233.     gmt &= 0x00FFFFFF;            // mask off dlsDelta
  234.     if (gmt & 0x00800000)        // if sign bit of gmtDelta is set
  235.         gmt |= 0xFF000000;        // then turn on high char if long
  236.     DrawHMS (gmt);
  237.  
  238.     EndUpdate( window );
  239. }
  240.  
  241.  
  242. void DrawHMS (long time)
  243. {
  244.     Str255    str;
  245.  
  246.     if (time<0)
  247.     {
  248.         time = -time;
  249.         DrawString("\p- ");
  250.     }
  251.  
  252.     NumToString( time / 3600L, str);
  253.     DrawString(str);
  254.     DrawString("\ph ");
  255.     
  256.     NumToString( (time % 3600L) / 60L, str);
  257.     DrawString(str);
  258.     DrawString("\pm ");
  259.  
  260. }
  261.  
  262.  
  263. void DrawDMS (long degr)
  264. {
  265.     Str255    str;
  266.     
  267.     if (degr<0)
  268.     {
  269.         degr = -degr;
  270.         DrawString("\p- ");
  271.     }
  272.  
  273.     NumToString( degr / 11930464L, str);
  274.     DrawString(str);
  275.     DrawString("\p° ");
  276.     
  277.     NumToString( (degr % 11930464L) / 198841L, str);
  278.     DrawString(str);
  279.     DrawString("\pm ");
  280. }